<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 产品类型
 * @author LT
 */
class Type extends Auth_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('goods/GoodsTypeModel', '' , true);
	}
	
	/**
	 * 列表
	 */
	function index($index=1,$size=20){
		$result = $this->GoodsTypeModel->select($index,$size);
		$data['list'] = $result['data'];
		$data['pages'] = $this->_pages('/manager/goods/type/index',$result['total'],$size,$index);
		$data['action'] = 'list';
		$this->_view('manager/goods/type',$data);
	}

	/**
	 * 检测名称是否存在
	 * @param 名称 $name
	 */
	function isExistName($name) {
		$name = urldecode($name);
		$data['state'] = $this->GoodsTypeModel->count(array('name'=>$name));
		$this->_output_json($data);
	}
	
	/**
	 * 产品类型选择
	 */
	function choice(){
		$data['list'] = $this->GoodsTypeModel->selectAll();
		$this->_view('manager/goods/type_choice',$data);
	}
	
	/**
	 * 新增
	 */
	function add() {
		$data['action'] = 'add';
		$this->_view('manager/goods/type',$data);
	}
	
	function doadd() {
		$type = $this->input->post('type');
		$type['attr'] = serialize($type['attr']);
		$data['state'] = $this->GoodsTypeModel->insert($type);
		
		$data['message'] = $data['state'] > 0? '新增产品类型成功。':'新增产品类型失败！';
		$this->_output_json($data);
	}
	/**
	 * 编辑
	 */
	function edit($id=0) {
		$data['action'] = 'edit';
		$type = $this->GoodsTypeModel->selectById($id);
		$type['attr'] = unserialize($type['attr']);
		$data['type'] = $type;
		$this->_view('manager/goods/type',$data);
	}
	
	function doedit() {
		$type = $this->input->post('type');
		$type['attr'] = serialize($type['attr']);
		$data['state'] = $this->GoodsTypeModel->update($type);
		
		$data['message'] = $data['state'] > 0 ? '编辑产品类型成功。':'编辑产品类型失败！';
		$this->_output_json($data);
	}
	
	function dodelete($id) {
		$data['state'] = $this->GoodsTypeModel->delete($id);
		$data['message'] = $data['state'] > 0 ? '删除产品类型成功。':'删除产品类型失败！';
		$this->_output_json($data);
	}
}
?>
